package com.tarena.sentinel.demo01.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.tarena.sentinel.demo01.handler.block.GlobalBlockHandler;
import com.tarena.sentinel.demo01.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    /*@SentinelResource(
            value="resource01",
            blockHandler = "res01Block",
            blockHandlerClass = GlobalBlockHandler.class)*/
    public String sayHi(String name){
        long start=new Date().getTime();
        String result=helloService.sayHi(name);
        long end=new Date().getTime();
        System.out.println("调用资源方法时间间隔:"+(end-start)+"ms");
        return result;
    }

    /*//实际上就是catch逻辑
    public String res01Block(String name,BlockException e){
        //处理降级逻辑
        System.out.println("异常类型:"+e.getClass().getName());
        return "你的请求被拦截了,blockHandler";
    }*/
}
